home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / editors / emacs / xemacs / xemacs-1.006 / xemacs-1 / lib / xemacs-19.13 / lisp / packages / refbib.el < prev    next >
Encoding:
Text File  |  1995-03-25  |  22.9 KB  |  718 lines

  1. ;;; refbib.el --- convert refer-style references to ones usable by Latex bib
  2. ;; Keywords: bib, tex
  3.  
  4. ;; Copyright (C) 1989 Free Software Foundation, Inc.
  5.  
  6. ;; This file is part of XEmacs.
  7.  
  8. ;; XEmacs is free software; you can redistribute it and/or modify it
  9. ;; under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation; either version 2, or (at your option)
  11. ;; any later version.
  12.  
  13. ;; XEmacs is distributed in the hope that it will be useful, but
  14. ;; WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16. ;; General Public License for more details.
  17.  
  18. ;; You should have received a copy of the GNU General Public License
  19. ;; along with XEmacs; see the file COPYING.  If not, write to the Free
  20. ;; Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  
  22. ;; Use: from a buffer containing the refer-style bibliography,
  23. ;;   M-x r2b-convert-buffer
  24. ;; Program will prompt for an output buffer name, and will log
  25. ;; warnings during the conversion process in the buffer *Log*.
  26.  
  27. ; HISTORY
  28. ; 9/88, created
  29. ; modified 1/19/89, allow books with editor but no author;
  30. ;                   added %O ordering field;
  31. ;                   appended illegal multiple fields, instead of 
  32. ;                     discarding;
  33. ;                   added rule, a tech report whose %R number
  34. ;                     contains "ISBN" is really a book
  35. ;                   added rule, anything with an editor is a book
  36. ;                     or a proceedings
  37. ;                   added 'manual type, for items with institution
  38. ;                     but no author or editor
  39. ;                   fixed bug so trailing blanks are trimmed
  40. ;                   added 'proceedings type
  41. ;                   used "organization" field for proceedings
  42. ; modified 2/16/89, updated help messages
  43. ; modified 2/23/89, include capitalize stop words in r2b stop words,
  44. ;                   fixed problems with contractions (e.g. it's),
  45. ;                   caught multiple stop words in a row
  46. ; modified 3/1/89,  fixed capitialize-title for first words all caps
  47. ; modified 3/15/89, allow use of " to delimit fields
  48. ; modified 4/18/89, properly "quote" special characters on output
  49. (provide 'refer-to-bibtex)
  50. ;**********************************************************
  51. ; User Parameters
  52.  
  53. (defvar r2b-trace-on nil "*trace conversion")
  54.  
  55. (defvar r2b-journal-abbrevs
  56.    '(  
  57.        )
  58.    "  Abbreviation list for journal names.  
  59. If the car of an element matches a journal name exactly, it is replaced by
  60. the cadr when output.  Braces must be included if replacement is a
  61. {string}, but not if replacement is a bibtex abbreviation.  The cadr
  62. may be eliminated if is exactly the same as the car.  
  63.   Because titles are capitalized before matching, the abbreviation
  64. for the journal name should be listed as beginning with a capital 
  65. letter, even if it really doesn't.
  66.   For example, a value of '((\"Aij\" \"{Artificial Intelligence}\")
  67. (\"Ijcai81\" \"ijcai7\")) would expand Aij to the text string
  68. \"Artificial Intelligence\", but would replace Ijcai81 with the 
  69. BibTeX macro \"ijcai7\".")
  70.  
  71. (defvar r2b-booktitle-abbrevs 
  72.    '(  
  73.        )
  74.    "  Abbreviation list for book and proceedings names.  If the car of
  75. an element matches a title or booktitle exactly, it is replaced by 
  76. the cadr when output.  Braces must be included if replacement is 
  77. a {string}, but not if replacement is a bibtex abbreviation.  The cadr 
  78. may be eliminated if is exactly the same as the car.  
  79.   Because titles are capitalized before matching, the abbreviated title
  80. should be listed as beginning with a capital letter, even if it doesn't.
  81.   For example, a value of '((\"Aij\" \"{Artificial Intelligence}\")
  82. (\"Ijcai81\" \"ijcai7\")) would expand Aij to the text string
  83. \"Artificial Intelligence\", but would replace Ijcai81 with the 
  84. BibTeX macro \"ijcai7\".")
  85.  
  86. (defvar r2b-proceedings-list
  87.    '()
  88.    "  Assoc list of books or journals which are really conference proceedings,
  89. but whose name and whose abbrev expansion (as defined in r2b-journal-abbrevs
  90. and r2b-booktitle-abbrevs) does not contain the words 'conference' or
  91. 'proceedings'.  (Those cases are handled automatically.)
  92. The entry must match the given data exactly.
  93.   Because titles are capitalized before matching, the items in this list 
  94. should begin with a capital letter.
  95.   For example, suppose the title \"Ijcai81\" is used for the proceedings of
  96. a conference, and it's expansion is the BibTeX macro \"ijcai7\".  Then 
  97. r2b-proceedings-list should be '((\"Ijcai81\") ...).  If instead its 
  98. expansion were \"Proceedings of the Seventh International Conference
  99. on Artificial Intelligence\", then you would NOT need to include Ijcai81 
  100. in r2b-proceedings-list (although it wouldn't cause an error).")
  101.  
  102. (defvar r2b-additional-stop-words
  103.      "Some\\|What"
  104.    "Words other than the capitialize-title-stop-words
  105. which are not to be used to build the citation key")
  106.  
  107.  
  108. (defvar r2b-delimit-with-quote
  109.   t
  110.   "*If true, then use \" to delimit fields, otherwise use braces")
  111.  
  112. ;**********************************************************
  113. ; Utility Functions
  114.  
  115. (defvar capitalize-title-stop-words
  116.    (concat
  117.       "the\\|and\\|of\\|is\\|a\\|an\\|of\\|for\\|in\\|to\\|in\\|on\\|at\\|"
  118.       "by\\|with\\|that\\|its")
  119.    "Words not to be capitialized in a title (unless they are the first
  120. word in the title)")
  121.  
  122. (defvar capitalize-title-stop-regexp
  123.    (concat "\\(" capitalize-title-stop-words "\\)\\(\\b\\|'\\)"))
  124.  
  125. (defun capitalize-title-region (begin end)
  126.    "Like capitalize-region, but don't capitalize stop words, except the first"
  127.    (interactive "r")
  128.    (let ((case-fold-search nil) (orig-syntax-table (syntax-table)))
  129.       (unwind-protect
  130.      (save-restriction
  131.         (set-syntax-table text-mode-syntax-table)
  132.         (narrow-to-region begin end)
  133.         (goto-char (point-min))
  134.         (if (looking-at "[A-Z][a-z]*[A-Z]")
  135.            (forward-word 1)
  136.            (capitalize-word 1))
  137.         (while (re-search-forward "\\<" nil t)
  138.            (if (looking-at "[A-Z][a-z]*[A-Z]")
  139.           (forward-word 1)
  140.           (if (let ((case-fold-search t))
  141.              (looking-at capitalize-title-stop-regexp))
  142.              (downcase-word 1)
  143.              (capitalize-word 1)))
  144.            ))
  145.      (set-syntax-table orig-syntax-table))))
  146.  
  147.  
  148. (defun capitalize-title (s)
  149.    "Like capitalize, but don't capitalize stop words, except the first"
  150.    (save-excursion
  151.       (set-buffer (get-buffer-create "$$$Scratch$$$"))
  152.       (erase-buffer)
  153.       (insert s)
  154.       (capitalize-title-region (point-min) (point-max))
  155.       (buffer-string)))
  156.  
  157. ;*********************************************************
  158. (defun r2b-reset ()
  159.    "unbind defvars, for debugging"
  160.    (interactive)
  161.    (makunbound 'r2b-journal-abbrevs)
  162.    (makunbound 'r2b-booktitle-abbrevs)
  163.    (makunbound 'r2b-proceedings-list)
  164.    (makunbound 'capitalize-title-stop-words)
  165.    (makunbound 'capitalize-title-stop-regexp)
  166.    (makunbound 'r2b-additional-stop-words)
  167.    (makunbound 'r2b-stop-regexp)
  168.    )
  169.  
  170. (defvar r2b-stop-regexp
  171.    (concat "\\`\\(\\(" 
  172.       r2b-additional-stop-words "\\|" capitalize-title-stop-words
  173.       "\\)\\('\\w*\\)?\\W+\\)*\\([A-Z0-9]+\\)"))
  174.  
  175.  
  176. (defun r2b-trace (&rest args)
  177.    (if r2b-trace-on
  178.       (progn
  179.      (apply (function message) args)
  180.      (sit-for 0)
  181.      )))
  182.  
  183. (defun r2b-match (exp)
  184.    "returns string matched in current buffer"
  185.    (buffer-substring (match-beginning exp) (match-end exp)))
  186.  
  187. (defvar r2b-out-buf-name "*Out*" "*output from refer-to-bibtex" )
  188. (defvar r2b-log-name "*Log*" "*logs errors from refer-to-bibtex" )
  189. (defvar r2b-in-buf nil)
  190. (defvar r2b-out-buf nil)
  191. (defvar r2b-log nil)
  192.  
  193. (defvar r2b-error-found nil)
  194.  
  195. (setq r2b-variables '(
  196.             r2b-error-found
  197.               r2bv-author
  198.               r2bv-primary-author
  199.               r2bv-date
  200.               r2bv-year
  201.               r2bv-decade
  202.               r2bv-month
  203.               r2bv-title
  204.               r2bv-title-first-word
  205.               r2bv-editor
  206.               r2bv-annote
  207.               r2bv-tr
  208.               r2bv-address
  209.               r2bv-institution
  210.               r2bv-keywords
  211.               r2bv-booktitle
  212.               r2bv-journal
  213.               r2bv-volume
  214.               r2bv-number
  215.               r2bv-pages
  216.               r2bv-booktitle
  217.               r2bv-kn
  218.               r2bv-publisher
  219.               r2bv-organization
  220.               r2bv-school
  221.               r2bv-type
  222.               r2bv-where
  223.               r2bv-note
  224.               r2bv-ordering
  225.               ))
  226.  
  227. (defun r2b-clear-variables ()
  228.    "set all global vars used by r2b to nil"
  229.    (let ((vars r2b-variables))
  230.       (while vars
  231.      (set (car vars) nil)
  232.      (setq vars (cdr vars)))
  233.       ))
  234.  
  235. (defun r2b-warning (&rest args)
  236.    (setq r2b-error-found t)
  237.    (princ (apply (function format) args) r2b-log)
  238.    (princ "\n" r2b-log)
  239.    (princ "\n" r2b-out-buf)
  240.    (princ "% " r2b-out-buf)
  241.    (princ (apply (function format) args) r2b-out-buf)
  242.    )
  243.  
  244. (defun r2b-get-field (var field &optional unique required capitalize)
  245.    "Set VAR to string value of FIELD, if any.  If none, VAR is set to
  246. nil.  If multiple fields appear, then separate values with the
  247. '\\nand\\t\\t', unless UNIQUE is non-nil, in which case log a warning
  248. and just concatenate the values.  Trim off leading blanks and tabs on
  249. first line, and trailing blanks and tabs of every line.  Log a warning
  250. and set VAR to the empty string if REQUIRED is true.  Capitalize as a
  251. title if CAPITALIZE is true.  Returns value of VAR."
  252.    (let (item val (not-past-end t))
  253.       (r2b-trace "snarfing %s" field)
  254.       (goto-char (point-min))
  255.       (while (and not-past-end
  256.         (re-search-forward 
  257.            (concat "^" field "\\b[ \t]*\\(.*[^ \t\n]\\)[ \t]*") nil t))
  258.      (setq item (r2b-match 1))
  259.      (while (and (setq not-past-end (zerop (forward-line 1)))
  260.            (not (looking-at "[ \t]*$\\|%")))
  261.            (looking-at "\\(.*[^ \t\n]\\)[ \t]*$")
  262.            (setq item (concat item "\n" (r2b-match 1)))
  263.         )
  264.      (if (null val)
  265.         (setq val item)
  266.         (if unique
  267.            (progn
  268.           (r2b-warning "*Illegal multiple field %s %s" field item)
  269.           (setq val (concat val "\n" item))
  270.           )
  271.            (setq val (concat val "\n\t\tand " item))
  272.            )
  273.         )
  274.      )
  275.       (if (and val capitalize)
  276.      (setq val (capitalize-title val)))
  277.       (set var val)
  278.       (if (and (null val) required)
  279.      (r2b-require var))
  280.       ))
  281.  
  282. (defun r2b-set-match (var n regexp string )
  283.    "set VAR to the Nth subpattern in REGEXP matched by STRING, or nil if none"
  284.    (set var
  285.       (if (and (stringp string) (string-match regexp string))
  286.      (substring string (match-beginning n) (match-end n))
  287.      nil)
  288.       )
  289.    )
  290.  
  291. (defvar r2b-month-abbrevs
  292.    '(("jan") ("feb") ("mar") ("apr") ("may") ("jun") ("jul") ("aug")
  293.        ("sep") ("oct") ("nov") ("dec")))
  294.  
  295. (defun r2b-convert-month ()
  296.    "Try to convert r2bv-month to a standard 3 letter name"
  297.    (if r2bv-month
  298.       (let ((months r2b-month-abbrevs))
  299.      (if (string-match "[^0-9]" r2bv-month)
  300.         (progn
  301.            (while (and months (not (string-match (car (car months)) 
  302.                       r2bv-month)))
  303.           (setq months (cdr months)))
  304.            (if months
  305.           (setq r2bv-month (car (car months)))))
  306.         (progn
  307.            (setq months (car (read-from-string r2bv-month)))
  308.            (if (and (numberp months)
  309.               (> months 0)
  310.               (< months 13))
  311.           (setq r2bv-month (car (nth months r2b-month-abbrevs)))
  312.           (progn
  313.              (r2b-warning "* Ridiculous month")
  314.              (setq r2bv-month nil))
  315.           ))
  316.         ))
  317.       )
  318.    )
  319.  
  320. (defun r2b-snarf-input ()
  321.    "parse buffer into global variables"
  322.    (let ((case-fold-search t))
  323.       (r2b-trace "snarfing...")
  324.       (sit-for 0)
  325.       (set-buffer r2b-in-buf)
  326.       (goto-char (point-min))
  327.       (princ "    " r2b-log)
  328.       (princ (buffer-substring (point) (progn (end-of-line) (point))) r2b-log)
  329.       (terpri r2b-log)
  330.  
  331.       (r2b-get-field 'r2bv-author "%A")
  332.       (r2b-get-field 'r2bv-editor "%E")
  333.       (cond
  334.      (r2bv-author
  335.         (r2b-set-match 'r2bv-primary-author 1
  336.            "\\b\\(\\w+\\)[ \t]*\\($\\|,\\)" r2bv-author)
  337.         )
  338.      (r2bv-editor
  339.         (r2b-set-match 'r2bv-primary-author 1
  340.            "\\b\\(\\w+\\)[ \t]*\\($\\|,\\)" r2bv-editor)
  341.         )
  342.      (t
  343.         (setq r2bv-primary-author "")
  344.         )
  345.      )
  346.  
  347.       (r2b-get-field 'r2bv-date "%D" t t)
  348.       (r2b-set-match 'r2bv-year 0 "[12][0-9][0-9][0-9]" r2bv-date)
  349.       (and (null r2bv-year)
  350.      (r2b-set-match 'r2bv-year 1 "[^0-9]\\([0-9][0-9]\\)$" r2bv-date)
  351.      (setq r2bv-year (concat "19" r2bv-year)))
  352.       (r2b-set-match 'r2bv-decade 1 "..\\(..\\)" r2bv-year)
  353.       (r2b-set-match 'r2bv-month 0
  354.      "[0-9]+/\\|[a-zA-Z]+" r2bv-date)
  355.       (if (and (stringp r2bv-month) (string-match "\\(.*\\)/$" r2bv-month))
  356.      (setq r2bv-month (substring r2bv-month 0 (match-end 1))))
  357.       (r2b-convert-month)
  358.  
  359.       (r2b-get-field 'r2bv-title "%T" t t t)
  360.       (r2b-set-match 'r2bv-title-first-word 4
  361.      r2b-stop-regexp
  362.      r2bv-title)
  363.       
  364.       (r2b-get-field 'r2bv-annote "%X" t )
  365.       (r2b-get-field 'r2bv-tr "%R" t)
  366.       (r2b-get-field 'r2bv-address "%C" t)
  367.       (r2b-get-field 'r2bv-institution "%I" t)
  368.       (r2b-get-field 'r2bv-keywords "%K")
  369.       (r2b-get-field 'r2bv-booktitle "%B" t nil t)
  370.       (r2b-get-field 'r2bv-journal "%J" t nil t)
  371.       (r2b-get-field 'r2bv-volume "%V" t)
  372.       (r2b-get-field 'r2bv-number "%N" t)
  373.       (r2b-get-field 'r2bv-pages "%P" t)
  374.       (r2b-get-field 'r2bv-where "%W" t)
  375.       (r2b-get-field 'r2bv-ordering "%O" t)
  376.       )
  377.    )
  378.  
  379.  
  380. (defun r2b-put-field (field data &optional abbrevs)
  381.   "print bibtex FIELD = {DATA} if DATA not null; precede
  382. with a comma and newline; if ABBREVS list is given, then
  383. try to replace the {DATA} with an abbreviation"
  384.   (if data
  385.     (let (match nodelim multi-line index)
  386.       (cond
  387.     ((and abbrevs (setq match (assoc data abbrevs)))
  388.       (if (null (cdr match))
  389.         (setq data (car match))
  390.         (setq data (car (cdr match))))
  391.       (setq nodelim t))
  392.     ((and (not (equal data ""))
  393.         (not (string-match "[^0-9]" data)))
  394.       (setq nodelim t))
  395.     (t
  396.       (setq index 0)
  397.       (while (string-match "[\\~^]" data index)
  398.         (setq data (concat (substring data 0 (match-beginning 0))
  399.              "\\verb+"
  400.              (substring data (match-beginning 0) (match-end 0))
  401.              "+"
  402.              (substring data (match-end 0))))
  403.         (setq index (+ (match-end 0) 7)))
  404.       (setq index 0)
  405.       (while (string-match "[$&%#_{}]" data index)
  406.         (setq data (concat (substring data 0 (match-beginning 0))
  407.              "\\"
  408.              (substring data (match-beginning 0))))
  409.         (setq index (+ (match-end 0) 1)))
  410.       (setq index 0)
  411.       (if r2b-delimit-with-quote
  412.         (while (string-match "\"" data index)
  413.           (setq data (concat (substring data 0 (match-beginning 0))
  414.                "{\"}"
  415.                (substring data (match-end 0))))
  416.           (setq index (+ (match-end 0) 2))))
  417.         ))
  418.       (princ ", \n  ")
  419.       (princ field)
  420.       (princ " =\t")
  421.       (if (not nodelim) 
  422.     (if r2b-delimit-with-quote
  423.       (princ "\"")
  424.       (princ "{")))
  425.       (string-match ".*" data)
  426.       (if (> (match-end 0) 59)
  427.     (princ "\n"))
  428.       (princ data)
  429.       (if (not nodelim) 
  430.     (if r2b-delimit-with-quote
  431.       (princ "\"")
  432.       (princ "}")))
  433.       )
  434.     ))
  435.  
  436.  
  437. (defun r2b-require (vars)
  438.    "If any of VARS is null, set to empty string and log error"
  439.    (cond 
  440.       ((null vars))
  441.       ((listp vars) (r2b-require (car vars)) (r2b-require (cdr vars)))
  442.       (t
  443.      (if (null (symbol-value vars))
  444.         (progn
  445.            (r2b-warning "*Missing value for field %s" vars)
  446.            (set vars "")
  447.            )))
  448.       )
  449.    )
  450.  
  451.  
  452. (defmacro r2b-moveq (new old)
  453.    "set NEW to OLD and set OLD to nil"
  454.    (list 'progn (list 'setq new old) (list 'setq old 'nil)))
  455.  
  456. (defun r2b-isa-proceedings (name)
  457.    "return t if NAME is the name of proceedings"
  458.    (and
  459.       name
  460.       (or
  461.      (string-match "proceedings\\|conference" name)
  462.      (assoc name r2b-proceedings-list)
  463.      (let ((match (assoc name r2b-booktitle-abbrevs)))
  464.         (and match
  465.            (string-match "proceedings\\|conference" (car (cdr match)))))
  466.       )))
  467.  
  468. (defun r2b-isa-university (name)
  469.    "return t if NAME is a university or similar organization, 
  470. but not a publisher"
  471.    (and 
  472.       name
  473.       (string-match "university" name)
  474.       (not (string-match "press" name))
  475.  
  476.    ))
  477.  
  478. (defun r2b-barf-output ()
  479.    "generate bibtex based on global variables"
  480.    (let ((standard-output r2b-out-buf) (case-fold-search t) match)
  481.  
  482.       (r2b-trace "...barfing")
  483.       (sit-for 0)
  484.       (set-buffer r2b-out-buf)
  485.  
  486.       (setq r2bv-kn (concat r2bv-primary-author r2bv-decade
  487.             r2bv-title-first-word))
  488.       
  489.       (setq r2bv-entry-kind
  490.      (cond 
  491.         ((r2b-isa-proceedings r2bv-journal)
  492.            (r2b-moveq r2bv-booktitle r2bv-journal)
  493.            (if (r2b-isa-university r2bv-institution)
  494.           (r2b-moveq r2bv-organization r2bv-institution)
  495.           (r2b-moveq r2bv-publisher r2bv-institution))
  496.            (r2b-moveq r2bv-note r2bv-tr)
  497.            (r2b-require 'r2bv-author)
  498.            'inproceedings)
  499.         ((r2b-isa-proceedings r2bv-booktitle)
  500.            (if (r2b-isa-university r2bv-institution)
  501.           (r2b-moveq r2bv-organization r2bv-institution)
  502.           (r2b-moveq r2bv-publisher r2bv-institution))
  503.            (r2b-moveq r2bv-note r2bv-tr)
  504.            (r2b-require 'r2bv-author)
  505.            'inproceedings)
  506.         ((and r2bv-tr (string-match "phd" r2bv-tr))
  507.            (r2b-moveq r2bv-school r2bv-institution)
  508.            (r2b-require 'r2bv-school )
  509.            (r2b-require 'r2bv-author)
  510.            'phdthesis)
  511.         ((and r2bv-tr (string-match "master" r2bv-tr))
  512.            (r2b-moveq r2bv-school r2bv-institution)
  513.            (r2b-require 'r2bv-school )
  514.            (r2b-require 'r2bv-author)
  515.            'mastersthesis)
  516.         ((and r2bv-tr (string-match "draft\\|unpublish" r2bv-tr))
  517.            (r2b-moveq r2bv-note r2bv-institution)
  518.            (r2b-require 'r2bv-author)
  519.            'unpublished)
  520.         (r2bv-journal
  521.            (r2b-require 'r2bv-author)
  522.            'article)
  523.         (r2bv-booktitle
  524.            (r2b-moveq r2bv-publisher r2bv-institution)
  525.            (r2b-moveq r2bv-note r2bv-tr)
  526.            (r2b-require 'r2bv-publisher)
  527.            (r2b-require 'r2bv-author)
  528.            'incollection)
  529.         ((and r2bv-author
  530.         (null r2bv-editor)
  531.         (string-match "\\`personal communication\\'" r2bv-title))
  532.            'misc)
  533.         ((r2b-isa-proceedings r2bv-title)
  534.            (if (r2b-isa-university r2bv-institution)
  535.           (r2b-moveq r2bv-organization r2bv-institution)
  536.           (r2b-moveq r2bv-publisher r2bv-institution))
  537.            (r2b-moveq r2bv-note r2bv-tr)
  538.            'proceedings)
  539.         ((or r2bv-editor
  540.         (and r2bv-author
  541.            (or 
  542.               (null r2bv-tr)
  543.               (string-match "\\bisbn\\b" r2bv-tr))))
  544.            (r2b-moveq r2bv-publisher r2bv-institution)
  545.            (r2b-moveq r2bv-note r2bv-tr)
  546.            (r2b-require 'r2bv-publisher)
  547.            (if (null r2bv-editor)
  548.           (r2b-require 'r2bv-author))
  549.            'book)
  550.         (r2bv-tr
  551.            (r2b-require 'r2bv-institution)
  552.            (if (string-match 
  553.               "\\`\\(\\(.\\|\n\\)+\\)[ \t\n]+\\([^ \t\n]\\)+\\'" 
  554.               r2bv-tr)
  555.           (progn
  556.              (setq r2bv-type (substring r2bv-tr 0 (match-end 1)))
  557.              (setq r2bv-number (substring r2bv-tr 
  558.                       (match-beginning 3)))
  559.              (setq r2bv-tr nil))
  560.           (r2b-moveq r2bv-number r2bv-tr))
  561.            (r2b-require 'r2bv-author)
  562.            'techreport)
  563.         (r2bv-institution
  564.            (r2b-moveq r2bv-organization r2bv-institution)
  565.            'manual)
  566.         (t
  567.            'misc)
  568.         ))
  569.  
  570.       (r2b-require '( r2bv-year))
  571.  
  572.       (if r2b-error-found
  573.      (princ "\n% Warning -- Errors During Conversion Next Entry\n"))
  574.  
  575.       (princ "\n@")
  576.       (princ r2bv-entry-kind)
  577.       (princ "( ")
  578.       (princ r2bv-kn)
  579.  
  580.       (r2b-put-field "author" r2bv-author )
  581.       (r2b-put-field "title" r2bv-title r2b-booktitle-abbrevs)
  582.       (r2b-put-field "year" r2bv-year )
  583.  
  584.       (r2b-put-field "month" r2bv-month r2b-month-abbrevs)
  585.       (r2b-put-field "journal" r2bv-journal r2b-journal-abbrevs)
  586.       (r2b-put-field "volume" r2bv-volume)
  587.       (r2b-put-field "type" r2bv-type)
  588.       (r2b-put-field "number" r2bv-number)
  589.       (r2b-put-field "booktitle" r2bv-booktitle r2b-booktitle-abbrevs)
  590.       (r2b-put-field "editor" r2bv-editor)
  591.       (r2b-put-field "publisher" r2bv-publisher)
  592.       (r2b-put-field "institution" r2bv-institution)
  593.       (r2b-put-field "organization" r2bv-organization)
  594.       (r2b-put-field "school" r2bv-school)
  595.       (r2b-put-field "pages" r2bv-pages)
  596.       (r2b-put-field "address" r2bv-address)
  597.       (r2b-put-field "note" r2bv-note)
  598.       (r2b-put-field "keywords" r2bv-keywords)
  599.       (r2b-put-field "where" r2bv-where)
  600.       (r2b-put-field "ordering" r2bv-ordering)
  601.       (r2b-put-field "annote" r2bv-annote)
  602.  
  603.       (princ " )\n")
  604.       )
  605.    )
  606.  
  607.  
  608. (defun r2b-convert-record (output-name)
  609.    "transform current bib entry and append to buffer OUTPUT;
  610. do M-x r2b-help for more info"
  611.    (interactive 
  612.       (list (read-string "Output to buffer: " r2b-out-buf-name)))
  613.    (let (rec-end rec-begin not-done)
  614.       (setq r2b-out-buf-name output-name)
  615.       (setq r2b-out-buf (get-buffer-create output-name))
  616.       (setq r2b-in-buf (current-buffer))
  617.       (set-buffer r2b-out-buf)
  618.       (goto-char (point-max))
  619.       (setq r2b-log (get-buffer-create r2b-log-name))
  620.       (set-buffer r2b-log)
  621.       (goto-char (point-max))
  622.       (set-buffer r2b-in-buf)
  623.       (setq not-done (re-search-forward "[^ \t\n]" nil t))
  624.       (if not-done
  625.      (progn
  626.         (re-search-backward "^[ \t]*$" nil 2)
  627.         (re-search-forward "^%")
  628.         (beginning-of-line nil)
  629.         (setq rec-begin (point))
  630.         (re-search-forward "^[ \t]*$" nil 2)
  631.         (setq rec-end (point))
  632.         (narrow-to-region rec-begin rec-end)
  633.         (r2b-clear-variables)
  634.         (r2b-snarf-input)
  635.         (r2b-barf-output)
  636.         (set-buffer r2b-in-buf)
  637.         (widen)
  638.         (goto-char rec-end)
  639.         t)
  640.      nil
  641.      )
  642.       ))
  643.       
  644.       
  645. (defun r2b-convert-buffer (output-name)
  646.    "transform current buffer and append to buffer OUTPUT;
  647. do M-x r2b-help for more info"
  648.    (interactive 
  649.       (list (read-string "Output to buffer: " r2b-out-buf-name)))
  650.    (save-excursion
  651.       (setq r2b-log (get-buffer-create r2b-log-name))
  652.       (set-buffer r2b-log)
  653.       (erase-buffer))
  654.    (widen)
  655.    (goto-char (point-min))
  656.    (message "Working, please be patient...")
  657.    (sit-for 0)
  658.    (while (r2b-convert-record output-name) t)
  659.    (message "Done, results in %s, errors in %s" 
  660.       r2b-out-buf-name r2b-log-name)
  661.    )
  662.  
  663. (defvar r2b-load-quietly nil "*Don't print help message when loaded")
  664.  
  665. (defvar r2b-help-message
  666. "                   Refer to Bibtex Bibliography Conversion
  667.  
  668. A refer-style database is of the form:
  669.  
  670. %A Joe Blow
  671. %T Great Thoughts I've Thought
  672. %D 1977
  673. etc.
  674.  
  675. This utility converts these kind of databases to bibtex form, for
  676. users of TeX and LaTex.  Instructions:
  677. 1.  Visit the file containing the refer-style database.
  678. 2.  The command
  679.     M-x r2b-convert-buffer
  680.     converts the entire buffer, appending it's output by default in a
  681.     buffer named *Out*, and logging progress and errors in a buffer
  682.     named *Log*.  The original file is never modified.
  683.     Note that results are appended to *Out*, so if that buffer
  684.     buffer already exists and contains material you don't want to
  685.      save, you should kill it first.
  686. 3.  Switch to the buffer *Out* and save it as a named file.
  687. 4.  To convert a single refer-style entry, simply position the cursor
  688.     at the entry and enter
  689.     M-x r2b-convert-record
  690.     Again output is appended to *Out* and errors are logged in *Log*.
  691.  
  692. This utility is very robust and pretty smart about determining the
  693. type of the entry.  It includes facilities for expanding refer macros
  694. to text, or substituting bibtex macros.  Do M-x describe-variable on
  695.      r2b-journal-abbrevs
  696.      r2b-booktitle-abbrevs
  697.      r2b-proceedings-list
  698. for information on these features.
  699.  
  700. If you don't want to see this help message when you load this utility,
  701. then include the following line in your .emacs file:
  702.     (setq r2b-load-quietly t)
  703. To see this message again, perform 
  704.          M-x r2b-help")
  705.  
  706.  
  707. (defun r2b-help ()
  708.    "print help message"
  709.    (interactive)
  710.    (with-output-to-temp-buffer "*Help*"
  711.       (princ r2b-help-message)))
  712.  
  713. (if (not r2b-load-quietly)
  714.    (r2b-help))
  715.  
  716. (message "r2b loaded")
  717.  
  718.